package com.greenhouse9.prog07;

import java.util.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XmlObject {

    public String name;
    public short  type;
    public String value;
    public ArrayList<XmlObject> child = null;

    public void printR(int indent){
        print(indent);
        if (child != null) {
            for (int i=0; i< child.size(); i++){
                child.get(i).printR(indent + 4);
            }
        }
   }


    public void print(int indent){

        for (int i=0; i < indent ; i++){
            System.out.print(" ");
        }

        System.out.println(name + " "  + value);

    }

    public void setXmlInfo ( Node node ){

        //XmlObject xo = new XmlObject();

        this.type = node.getNodeType();
        this.name = node.getNodeName();
        this.value = node.getNodeValue();

        ArrayList<XmlObject> vlist = new ArrayList<XmlObject> ();

        if(node.hasChildNodes()) {

            this.child = new ArrayList<XmlObject> ();

            NodeList list = node.getChildNodes();

            for ( int j = 0; j < list.getLength(); j++ ) {

                XmlObject xo = new XmlObject();
                xo.setXmlInfo(list.item(j));
                //vlist.add(getXmlInfo(list.item(j)));
                child.add(xo);

            }
        }
    }

    public String getFirstChildValue(){
        if (child != null){
            return child.get(0).value;
        }
        else {
            return null;
        }
    }
}
